function remove(arr, item) {
  let i = arr.indexOf(item)
  if (i > -1) {
    arr.splice(i, 1)
  }
}
export default {
  __events__: {},
  __once_evnets__: [],
  on(event_name, fn) {
    if ($.isFunction(fn)) {
      ;(this.__events__[event_name] || (this.__events__[event_name] = [])).push(fn)
    }
    return this
  },
  once(event_name, fn) {
    if ($.isFunction(fn)) {
      ;(this.__events__[event_name] || (this.__events__[event_name] = [])).push(fn)
      // 记录这个事件只触发一次
      if (this.__once_evnets__.indexOf(event_name) === -1) {
        this.__once_evnets__.push(event_name)
      }
    }
    return this
  },
  off(event_name, fn) {
    if (!this.__events__[event_name]) return
    remove(this.__events__[event_name], fn)
  },
  emit(event_name, val) {
    let hooks = this.__events__[event_name]
    if (!hooks) return
    for (let hook of hooks) {
      hook(val)
      let isOnceEvent = this.__once_evnets__.indexOf(event_name)
      if (isOnceEvent > -1) {
        this.off(event_name, hook)
        this.__once_evnets__.splice(isOnceEvent, 1)
      }
    }
  }
}
